using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using net_work.Data.Entity.Cou;
using net_work.Data.Enum.Cou;

namespace net_work.Config.DataBaseConfig.Cou;

/// <summary>
/// 课程实体配置类
/// </summary>
public class CourseEntityTypeConfiguration : IEntityTypeConfiguration<Course>
{
    public void Configure(EntityTypeBuilder<Course> builder)
    {
        builder.ToTable("cou_cou");

        builder.HasKey(c => c.Id);

        builder.HasIndex(c => c.CouCode).IsUnique();

        // 配置字段名映射
        builder.Property(c => c.Id).HasColumnName("cou_id");
        builder.Property(c => c.CouCode).HasColumnName("cou_code").IsRequired().HasMaxLength(64);
        builder.Property(c => c.CouName).HasColumnName("cou_name").IsRequired().HasMaxLength(64);
        builder.Property(c => c.CouDesc).HasColumnName("cou_desc").IsRequired().HasMaxLength(512);

        // 将枚举转换为字符串
        builder.Property(c => c.CouProp)
            .HasColumnName("cou_prop")
            .IsRequired()
            .HasMaxLength(64)
            .HasConversion(
                v => v.ToString(), // 枚举转字符串
                v => (CourseProp)Enum.Parse(typeof(CourseProp), v) // 字符串转枚举
            );

        builder.Property(c => c.CollegeId).HasColumnName("colle_id").IsRequired();

        builder.Property(c => c.CreatedBy).HasColumnName("create_by").IsRequired().HasMaxLength(64);
        builder.Property(c => c.CreatedTime).HasColumnName("create_time").IsRequired();
        builder.Property(c => c.UpdatedBy).HasColumnName("update_by").IsRequired().HasMaxLength(64);
        builder.Property(c => c.UpdatedTime).HasColumnName("update_time").IsRequired();
        builder.Property(c => c.Remark).HasColumnName("remark").HasMaxLength(256);
        builder.Property(c => c.DelFlag).HasColumnName("del_flag").HasDefaultValue('0');
    }
}